﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class CompanyGreenNumListControl : YuanlinUserControl,
        IReplyConverter<CompanyGreenNumReply, GetListOperation>
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private EntityList companyGreenNums;
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                CompanyGreenNumListRequest request = new CompanyGreenNumListRequest();
                request.PageIndex = pager.PageIndex;
                request.PageSize = pager.PageSize;
                request.UserId = user.EntityId.Value;
                request.GreenNum = "";
                requestBinders.Add(RequestHelper.BuildGetCompanyGreenNumListRequest(this, request));
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            if (list == null)
            {
                return;
            }

            list.DataSource = companyGreenNums;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(companyGreenNums.TotalCount);
                currentPage.TotalRecords = (companyGreenNums.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }


        #region IReplyConverter<CompanyGreenNumReply,GetListOperation> Members

        public void ConvertReply(CompanyGreenNumReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                companyGreenNums = reply.EntityList;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}

